'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：get_video_url.py
#日期：2018-11-24
#备注：获取excel文件中下载信息存入到列表，然后循环去取数据下载文件（mp4,mp3,jpg,pdf等）
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import xlrd
import urllib.request
import os

def get_excel_cell(xlsFile,num,nrows):
    data = xlrd.open_workbook(xlsFile)
    table = data.sheets()[0]
    cellData = []

    # 获取指定列数据
    for i in range(num, nrows):              # i=num处理，i=nrows不处理
        row = []
        className = table.cell_value(i, 3)   # 第4列课程名称
        row.append(className)
        classUrl = table.cell_value(i, 4)    # 第5列课程下载路径
        row.append(classUrl)

        cellData.append(row)
    return cellData


def get_video(folder,url,fileName,fileType):
    os.chdir(folder)                           # 切换到将要存放文件的目录
    file = open(fileName + fileType, "wb")     # 打开文件
    try:
        req = urllib.request.Request(url=url)
        req.add_header("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36")
        video = urllib.request.urlopen(req, timeout=40)
        mp4 = video.read()                     # 将文件转换为bytes格式
        file.write(mp4)                        # 文件写入
        print(type(file),type(req),type(video),type(mp4))
    except Exception as f:
        print(str(f))
    file.close()


if __name__ == '__main__':
    videoInfo = get_excel_cell('F:\PythonProject\Pacong\docs\yuyus185.xls',182,183)
    for i in range(len(videoInfo)):
        fileName = videoInfo[i][0]
        url = videoInfo[i][1]
        fileType = url[-4:]          # 截取最后4位，可以判断内容的类型（.jpg,.mp4,mp3等）
        print(fileName,fileType,url)
        get_video('F:\SoftwareTest\网盘-工作\宇信科技\在线学习\统一开发平台',url,fileName,fileType)